﻿Imports System.Windows.Forms
Imports System.Net

Public Class adddownload
    Private Sub adddownload_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AddHandler My.Computer.Network.NetworkAvailabilityChanged, AddressOf MyComputerNetwork_NetworkAvailabilityChanged
        If My.Computer.Network.IsAvailable = True Then
            status.Text = "Network Status : Connected"
        Else
            status.Text = "Network status : Disconnected"
        End If
    End Sub
    Private Sub MyComputerNetwork_NetworkAvailabilityChanged(ByVal sender As Object, ByVal e As Devices.NetworkAvailableEventArgs)
        If e.IsNetworkAvailable = True Then
            status.Text = "Network Status : Connected"
        Else
            status.Text = "Network status : Disconnected"
        End If
    End Sub
    Private Sub RadButton3_Click(sender As Object, e As EventArgs) Handles RadButton3.Click
        Me.Dispose()
    End Sub
    Private Sub RadButton2_Click(sender As Object, e As EventArgs) Handles RadButton2.Click
        On Error GoTo last
        If from.Text Or destination.Text <> "" Then
            Dim client As WebClient = New WebClient
            AddHandler client.DownloadProgressChanged, AddressOf client_ProgressChanged
            AddHandler client.DownloadFileCompleted, AddressOf client_DownloadCompleted
            client.DownloadFileAsync(New Uri(from.Text), destination.Text)
            downloadstatus.Text = "Download started"
        Else
            If from.Text = "" Then
                MsgBox("Source of the file to be downloaded in empty. Please verify", vbCritical, "Error - Surfer Vio ")
            Else
                MsgBox("Destination location is not entered! Please verify", vbExclamation, "Error - Surfer Vio")
            End If
        End If
        GoTo fun_end
last:
        MsgBox("Download has not been completed successfully. Please retry downloading.", vbExclamation, "Download Not completed - Surfer Vio")
fun_end:
    End Sub
    Private Sub client_ProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
        Dim bytesIn As Double = Double.Parse(e.BytesReceived.ToString())
        Dim totalBytes As Double = Double.Parse(e.TotalBytesToReceive.ToString())
        Dim percentage As Double = bytesIn / totalBytes * 100
        downloadprogress.Maximum = Int32.Parse(Math.Truncate(percentage).ToString())
        downloadprogress.Text = "Download progress : " + Convert.ToInt32(percentage).ToString + "%"
    End Sub
    Private Sub client_DownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
        downloadstatus.Text = "Download completed successfully"
    End Sub
End Class
